define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'unilive/goods/index' + location.search,
                    add_url: 'unilive/goods/add',
                    edit_url: 'unilive/goods/edit',
                    del_url: 'unilive/goods/del',
                    copy_url: 'unilive/goods/copy',
                    recall_url: 'unilive/goods/recall',
                    resubmit_url: 'unilive/goods/resubmit',
                    sync_url: 'unilive/goods/sync',
                    getgoodswarehouse_url: 'unilive/goods/getgoodswarehouse',
                    multi_url: 'unilive/goods/multi',
                    table: 'unilive_goods',
                }
            });

            // 合并操作方法
            $.extend(Table.api.events.operate,
                {
                    'click .btn-copy': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.copy_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Copy'), $(this).data() || {});
                    },
                    //重新提交审核
                    'click .btn-resubmit': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(
                            __('Are you sure you want to resubmit this item?'),
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                var table = $(that).closest('table');
                                var options = table.bootstrapTable('getOptions');

                                let params = {url: $.fn.bootstrapTable.defaults.extend.resubmit_url + '?ids=' + row[options.pk] };
                                Fast.api.ajax(params, function (data, ret) {
                                    table.bootstrapTable('refresh');
                                }, function (data, ret) {
                                    //
                                });

                                Layer.close(index);
                            }
                        );
                    },
                    // 撤回审核
                    'click .btn-recall': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(
                            __('Are you sure you want to recall this item?'),
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                var table = $(that).closest('table');
                                var options = table.bootstrapTable('getOptions');

                                let params = {url: $.fn.bootstrapTable.defaults.extend.recall_url + '?ids=' + row[options.pk] };
                                Fast.api.ajax(params, function (data, ret) {
                                    table.bootstrapTable('refresh');
                                }, function (data, ret) {
                                    //
                                });
                                Layer.close(index);
                            }
                        );
                    },
                    // 更新商品状态
                    'click .btn-getgoodswarehouse': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var that = this;
                        var table = $(that).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        let params = {url: $.fn.bootstrapTable.defaults.extend.getgoodswarehouse_url + '?ids=' + row[options.pk] };
                        Fast.api.ajax(params, function (data, ret) {
                            table.bootstrapTable('refresh');
                        }, function (data, ret) {
                            //
                        });
                    },
                }
            );

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goodsId', title: __('Goodsid')},
                        {field: 'coverImgUrl', title: __('Coverimgurl'),operate:false, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name')},
                        {field: 'priceType', title: __('Pricetype'), searchList: {"1":__('Pricetype 1'),"2":__('Pricetype 2'),"3":__('Pricetype 3')}, formatter: Table.api.formatter.status},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'price2', title: __('Price2'), operate:'BETWEEN'},
                        {field: 'url', title: __('Url')},
                        {field: 'auditId', title: __('Auditid'),visible:false},
                        {field: 'audit_status', title: __('Audit_status'), searchList: {"0":__('Audit_status 0'),"1":__('Audit_status 1'),"2":__('Audit_status 2'),"3":__('Audit_status 3')}, formatter: Table.api.formatter.status},
                        {field: 'third_party_tag',visible:false, title: __('Third_party_tag'), searchList: {"2":__('Third_party_tag 2'),"0":__('Third_party_tag 0')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', visible:false, title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'copy',
                                    text: __('Copy'),
                                    classname: 'btn btn-xs btn-info btn-copy',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-copy'
                                },
                                {
                                    name: 'recall',
                                    text: __('Recall'),
                                    classname: 'btn btn-xs btn-info btn-recall',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-times-rectangle-o'
                                },
                                {
                                    name: 'resubmit',
                                    text: __('Resubmit'),
                                    classname: 'btn btn-xs btn-info btn-resubmit',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-arrow-up'
                                },
                                {
                                    name: 'getgoodswarehouse',
                                    text: __('Getgoodswarehouse'),
                                    classname: 'btn btn-xs btn-info btn-getgoodswarehouse',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-circle-o-notch'
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');

            // 同步商品状态
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            $(toolbar).on('click', '.btn-getgoodswarehouse', function () {
                var ids = Table.api.selectedids(table);
                var options = {url: $.fn.bootstrapTable.defaults.extend.getgoodswarehouse_url + '?ids='+ids};
                Fast.api.ajax(options, function (data, ret) {
                    table.bootstrapTable('refresh');
                }, function (data, ret) {
                    //
                });
            });

            // 同步微信小程序后台商品
            $(toolbar).on('click', '.btn-sync', function () {
                var options = {url: $.fn.bootstrapTable.defaults.extend.sync_url};
                Fast.api.ajax(options, function (data, ret) {
                    table.bootstrapTable('refresh');
                }, function (data, ret) {
                    //
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        copy: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'unilive/goods/index',
                }
            });
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function () {
                return "请输入商品名称";
            };

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                placeholder: '请输入商品名称',
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'goodsId', title: __('Goodsid')},
                        {field: 'coverImgUrl', title: __('Coverimgurl'),operate:false, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name')},
                        {field: 'priceType', title: __('Pricetype'), searchList: {"1":__('Pricetype 1'),"2":__('Pricetype 2'),"3":__('Pricetype 3')}, formatter: Table.api.formatter.status},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'price2', title: __('Price2'), operate:'BETWEEN'},
                        {field: 'url', title: __('Url')},
                        {field: 'auditId', title: __('Auditid'),visible:false},
                        {field: 'audit_status', title: __('Audit_status'), searchList: {"0":__('Audit_status 0'),"1":__('Audit_status 1'),"2":__('Audit_status 2'),"3":__('Audit_status 3')}, formatter: Table.api.formatter.status},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    var dataArr = new Array();
                                    dataArr.push(row);
                                    Fast.api.close({data: dataArr, multiple: false});
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var dataArr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    dataArr.push(j);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;
                Fast.api.close({data: dataArr, multiple: true});
            });

            // 为表格绑定事件
            //Table.api.bindevent(table);
        }
    };
    return Controller;
});
